package com.web.userservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.web.userservice.entity.User;
import com.web.userservice.entity.UserVO;
import com.web.userservice.mapper.UserMapper;
import com.web.userservice.service.UserService;
import com.web.util.JwtUtils;
import com.web.util.RsaUtils;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public UserVO login(String username, String password) {
        //查询用户
        User user = userMapper.selectOne(new QueryWrapper<User>().lambda().eq(User::getUsername,username));
        //验证账号和密码
        if(user != null){
            if(password.equals(user.getPassword())){
                //验证成功，生成token字符串
                String token = JwtUtils.generateToken(username, RsaUtils.privateKey,JwtUtils.EXPIRE_MINUTES);
                //包装user和token到userVO中
                UserVO userVO = new UserVO();
                userVO.setUser(user);
                userVO.setToken(token);
                return userVO;
            }
        }
        return null;
    }
}
